package com.vegetable.modules.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.modules.entity.sys.SysMessageEntity;
import com.vegetable.modules.mapper.sys.SysMessageDao;
import com.vegetable.modules.service.sys.SysMessageService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 系统消息服务实现
 *
 * @author Qoder
 */
@Service("sysMessageService")
@AllArgsConstructor
public class SysMessageServiceImpl extends ServiceImpl<SysMessageDao, SysMessageEntity> implements SysMessageService {

    private final SysMessageDao sysMessageDao;

    @Override
    public Integer queryUnreadCount(Long receiverUserId) {
        return sysMessageDao.queryUnreadCount(receiverUserId);
    }

    @Override
    public IPage<SysMessageEntity> queryMessagePage(IPage<SysMessageEntity> page, Long receiverUserId) {
        return this.page(page, new QueryWrapper<SysMessageEntity>()
                .eq("receiver_user_id", receiverUserId)
                .orderByDesc("create_time"));
    }

    @Override
    public List<SysMessageEntity> queryUnreadMessageList(Long receiverUserId) {
        return sysMessageDao.queryUnreadMessageList(receiverUserId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean sendMessage(SysMessageEntity message) {
        message.setCreateTime(new Date());
        message.setIsRead(0); // 默认未读
        return this.save(message);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean sendBatchMessage(List<SysMessageEntity> messages) {
        Date now = new Date();
        for (SysMessageEntity message : messages) {
            message.setCreateTime(now);
            message.setIsRead(0); // 默认未读
        }
        return this.saveBatch(messages);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean markAsRead(Long messageId, Long receiverUserId) {
        SysMessageEntity message = this.getOne(new QueryWrapper<SysMessageEntity>()
                .eq("message_id", messageId)
                .eq("receiver_user_id", receiverUserId));
        if (message != null) {
            message.setIsRead(1);
            message.setReadTime(new Date());
            return this.updateById(message);
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean markBatchAsRead(List<Long> messageIds, Long receiverUserId) {
        List<SysMessageEntity> messages = this.list(new QueryWrapper<SysMessageEntity>()
                .in("message_id", messageIds)
                .eq("receiver_user_id", receiverUserId));
        Date now = new Date();
        for (SysMessageEntity message : messages) {
            message.setIsRead(1);
            message.setReadTime(now);
        }
        return this.updateBatchById(messages);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteMessage(Long messageId, Long receiverUserId) {
        return this.remove(new QueryWrapper<SysMessageEntity>()
                .eq("message_id", messageId)
                .eq("receiver_user_id", receiverUserId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatchMessage(List<Long> messageIds, Long receiverUserId) {
        return this.remove(new QueryWrapper<SysMessageEntity>()
                .in("message_id", messageIds)
                .eq("receiver_user_id", receiverUserId));
    }
}